import requests, re, json, time

session = requests.Session()
session.headers.update({
    'user-agent': 'Mozilla/5.0 (Linux; Android 10; SM-G973F) AppleWebKit/537.36 Chrome/122.0 Mobile'
})

# 1. 先拿 _token_
index = session.get('https://m.1688.com/', timeout=10)
index.raise_for_status()
token = re.search(r'"_token_":"(.*?)"', index.text).group(1)
print('got _token_:', token)

# 2. 再拼业务请求（把 _token_ 放 query 里）
url = (
    "https://h5api.m.1688.com/h5/mtop.relationrecommend.wirelessrecommend.recommend/2.0/"
    "?jsv=2.7.4"
    "&appKey=12574478"
    "&t=1762860393854"
    "&sign=9292eb3e2570e6d73d74f6bb1936c3a1"
    "&api=mtop.relationrecommend.WirelessRecommend.recommend"
    "&v=2.0"
    "&dataType=jsonp"
    "&type=jsonp"
    "&callback=mtopjsonpfetchTpp_32517_getOfferList4"
    f"&_token_={token}"        # ✅ 关键
)

payload = {
    "appId": 32517,
    "params": json.dumps({
        "verticalProductFlag": "pcmarket",
        "searchScene": "pcOfferSearch",
        "charset": "utf8",
        "beginPage": 1,
        "pageSize": 60,
        "keywords": "男装",
        "spm": "a260k.home2025.category.dL2.66333597bN5ZeO",
        "method": "getOfferList"
    }, separators=(',', ':'))
}

r = session.get(url, params=payload, timeout=10)
print('http code:', r.status_code)

# 3. 剥 JSONP 外壳
real_json = re.search(r'\((\{.*\})\)$', r.text.strip()).group(1)
data = json.loads(real_json)
print(data['ret'], '商品条数:', len(data['data'].get('offerList', [])))